<!-- This comment will put IE 6, 7 and 8 in quirks mode -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>include/shark/Algorithms/DirectSearch/Operators/Recombination/PartiallyMappedCrossover.h Source File</title>
<script type="text/javaScript" src="search/search.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3.0.1/es5/tex-mml-chtml.js"></script>
<script src="../../mlstyle.js"></script>
<link href="../css/besser.css" rel="stylesheet" type="text/css"/>
</head>
<!-- pretty cool: each body gets an id tag which is the basename of the web page  -->
<!--              and allows for page-specific CSS. this is client-side scripted, -->
<!--              so the id will not yet show up in the served source code -->
<script type="text/javascript">
    jQuery(document).ready(function () {
        var url = jQuery(location).attr('href');
        var pname = url.substr(url.lastIndexOf("/")+1, url.lastIndexOf(".")-url.lastIndexOf("/")-1);
        jQuery('#this_url').html('<strong>' + pname + '</strong>');
        jQuery('body').attr('id', pname);
    });
</script>
<body>
    <div id="shark_old">
        <div id="wrap">
            <div id="header">
                <div id="site-name"><a href="../../sphinx_pages/build/html/index.html">Shark machine learning library</a></div>
                <ul id="nav">
                    <li >
                        <a href="../../sphinx_pages/build/html/rest_sources/installation.html">Installation</a>
                    </li>
		    <li >
                        <a href="../../sphinx_pages/build/html/rest_sources/tutorials/tutorials.html">Tutorials</a>
                    </li>
		    <li >
                        <a href="../../sphinx_pages/build/html/rest_sources/benchmark.html">Benchmarks</a>
                    </li>
                    <li class="active">
                        <a href="classes.html">Documentation</a>
                        <ul>
                            <li class="first"></li>
                            <li><a href="../../sphinx_pages/build/html/rest_sources/quickref/quickref.html">Quick references</a></li>
                            <li><a href="classes.html">Class list</a></li>
                            <li class="last"><a href="group__shark__globals.html">Global functions</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </div>
<div id="doxywrapper">
<!--
    <div id="global_doxytitle">Doxygen<br>Documentation:</div>
-->
    <div id="navrow_wrapper">
<!-- Generated by Doxygen 1.9.8 -->
<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_d44c64559bbebec7f509842c48db8b23.html">include</a></li><li class="navelem"><a class="el" href="dir_9d0c4981f10d03078bcfd5c74fe41ce8.html">shark</a></li><li class="navelem"><a class="el" href="dir_24fc231769ada4cfc8add7cd238ad0f8.html">Algorithms</a></li><li class="navelem"><a class="el" href="dir_a8795d52992905c0ec88467e5ad28556.html">DirectSearch</a></li><li class="navelem"><a class="el" href="dir_2e5794c6cbad6b48f269b954d4889f28.html">Operators</a></li><li class="navelem"><a class="el" href="dir_0e614bd10c2af327af12ad48d674cea4.html">Recombination</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle"><div class="title">PartiallyMappedCrossover.h</div></div>
</div><!--header-->
<div class="contents">
<a href="_partially_mapped_crossover_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno">    1</span><span class="preprocessor">#ifndef SHARK_ALGORITHMS_DIRECT_SEARCH_OPERATORS_RECOMBINATION_PARTIALLYMAPPEDCROSSOVER_H</span></div>
<div class="line"><a id="l00002" name="l00002"></a><span class="lineno">    2</span><span class="preprocessor">#define SHARK_ALGORITHMS_DIRECT_SEARCH_OPERATORS_RECOMBINATION_PARTIALLYMAPPEDCROSSOVER_H</span></div>
<div class="line"><a id="l00003" name="l00003"></a><span class="lineno">    3</span> </div>
<div class="line"><a id="l00004" name="l00004"></a><span class="lineno">    4</span><span class="preprocessor">#include &lt;<a class="code" href="_random_8h.html">shark/Core/Random.h</a>&gt;</span></div>
<div class="line"><a id="l00005" name="l00005"></a><span class="lineno">    5</span><span class="preprocessor">#include &lt;<a class="code" href="_exception_8h.html">shark/Core/Exception.h</a>&gt;</span></div>
<div class="line"><a id="l00006" name="l00006"></a><span class="lineno">    6</span> </div>
<div class="line"><a id="l00007" name="l00007"></a><span class="lineno">    7</span><span class="keyword">namespace </span><a class="code hl_namespace" href="namespaceshark.html" title="AbstractMultiObjectiveOptimizer.">shark</a> {</div>
<div class="line"><a id="l00008" name="l00008"></a><span class="lineno">    8</span><span class="comment"></span> </div>
<div class="line"><a id="l00009" name="l00009"></a><span class="lineno">    9</span><span class="comment">/// @brief Implements partially mapped crossover</span></div>
<div class="line"><a id="l00010" name="l00010"></a><span class="lineno">   10</span><span class="comment">/// </span></div>
<div class="line"><a id="l00011" name="l00011"></a><span class="lineno">   11</span><span class="comment">/// PartiallyMappedCrossover recombines points representing</span></div>
<div class="line"><a id="l00012" name="l00012"></a><span class="lineno">   12</span><span class="comment">/// permutations, i.e. it ensures that the results are also permutations.</span></div>
<div class="foldopen" id="foldopen00013" data-start="{" data-end="};">
<div class="line"><a id="l00013" name="l00013"></a><span class="lineno"><a class="line" href="structshark_1_1_partially_mapped_crossover.html">   13</a></span><span class="comment"></span><span class="keyword">struct </span><a class="code hl_struct" href="structshark_1_1_partially_mapped_crossover.html" title="Implements partially mapped crossover.">PartiallyMappedCrossover</a> {</div>
<div class="line"><a id="l00014" name="l00014"></a><span class="lineno">   14</span><span class="comment"></span> </div>
<div class="line"><a id="l00015" name="l00015"></a><span class="lineno">   15</span><span class="comment">    /// \brief Mates the supplied individuals.</span></div>
<div class="line"><a id="l00016" name="l00016"></a><span class="lineno">   16</span><span class="comment">    /// </span></div>
<div class="line"><a id="l00017" name="l00017"></a><span class="lineno">   17</span><span class="comment">    /// \param [in] rng Random number generator.</span></div>
<div class="line"><a id="l00018" name="l00018"></a><span class="lineno">   18</span><span class="comment">    /// \param [in,out] individual1 Individual to be mated.</span></div>
<div class="line"><a id="l00019" name="l00019"></a><span class="lineno">   19</span><span class="comment">    /// \param [in,out] individual2 Individual to be mated.</span></div>
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno">   20</span><span class="comment"></span>    <span class="keyword">template</span>&lt;<span class="keyword">class</span> Rng, <span class="keyword">typename</span> Indiv<span class="keywordtype">id</span>ualType&gt;</div>
<div class="foldopen" id="foldopen00021" data-start="{" data-end="}">
<div class="line"><a id="l00021" name="l00021"></a><span class="lineno"><a class="line" href="structshark_1_1_partially_mapped_crossover.html#acfc097e51107f82f41430ada99a06530">   21</a></span>    <span class="keywordtype">void</span> <a class="code hl_function" href="structshark_1_1_partially_mapped_crossover.html#acfc097e51107f82f41430ada99a06530" title="Mates the supplied individuals.">operator()</a>(Rng&amp; rng, <a class="code hl_class" href="classshark_1_1_individual.html" title="Individual is a simple templated class modelling an individual that acts as a candidate solution in a...">IndividualType</a> &amp; individual1, <a class="code hl_class" href="classshark_1_1_individual.html" title="Individual is a simple templated class modelling an individual that acts as a candidate solution in a...">IndividualType</a> &amp; individual2 )<span class="keyword">const</span>{    </div>
<div class="line"><a id="l00022" name="l00022"></a><span class="lineno">   22</span>        <a class="code hl_define" href="_exception_8h.html#a42a6a50e4d06c00d60fbca5333f40768">SIZE_CHECK</a>(individual1.<a class="code hl_function" href="classshark_1_1_individual.html#a8d6ed0dfa38f5b0e9debdeb5529e6689" title="Returns a reference to the search point that is associated with the individual.">searchPoint</a>().size() == individual2.<a class="code hl_function" href="classshark_1_1_individual.html#a8d6ed0dfa38f5b0e9debdeb5529e6689" title="Returns a reference to the search point that is associated with the individual.">searchPoint</a>().size());</div>
<div class="line"><a id="l00023" name="l00023"></a><span class="lineno">   23</span>        </div>
<div class="line"><a id="l00024" name="l00024"></a><span class="lineno">   24</span>        <span class="keyword">typedef</span> <span class="keyword">typename</span> <a class="code hl_typedef" href="classshark_1_1_individual.html#aa96e1866f7225504ba5a69965dd9d768">IndividualType::SearchPointType</a> PointType;</div>
<div class="line"><a id="l00025" name="l00025"></a><span class="lineno">   25</span>        PointType&amp; t1 = individual1.<a class="code hl_function" href="classshark_1_1_individual.html#a8d6ed0dfa38f5b0e9debdeb5529e6689" title="Returns a reference to the search point that is associated with the individual.">searchPoint</a>();</div>
<div class="line"><a id="l00026" name="l00026"></a><span class="lineno">   26</span>        PointType&amp; t2 = individual2.<a class="code hl_function" href="classshark_1_1_individual.html#a8d6ed0dfa38f5b0e9debdeb5529e6689" title="Returns a reference to the search point that is associated with the individual.">searchPoint</a>();</div>
<div class="line"><a id="l00027" name="l00027"></a><span class="lineno">   27</span>        </div>
<div class="line"><a id="l00028" name="l00028"></a><span class="lineno">   28</span>        std::size_t n = t1.size();</div>
<div class="line"><a id="l00029" name="l00029"></a><span class="lineno">   29</span>        <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> unset = <span class="keyword">static_cast&lt;</span><span class="keywordtype">unsigned</span> <span class="keywordtype">int</span><span class="keyword">&gt;</span>(n + 1);</div>
<div class="line"><a id="l00030" name="l00030"></a><span class="lineno">   30</span>        </div>
<div class="line"><a id="l00031" name="l00031"></a><span class="lineno">   31</span>        </div>
<div class="line"><a id="l00032" name="l00032"></a><span class="lineno">   32</span>        <span class="comment">//compute cuttingpoints 0 &lt;= cuttingPoint1 &lt; cuttingPoint2 &lt;= n</span></div>
<div class="line"><a id="l00033" name="l00033"></a><span class="lineno">   33</span>        std::size_t cuttingPoint1 = <a class="code hl_function" href="namespaceshark_1_1random.html#aa64d4174eaf7111b03e0504eaa56b666" title="Draws a discrete number in {low,low+1,...,high} by drawing random numbers from rng.">random::discrete</a>(rng, std::size_t(0), n - 2);</div>
<div class="line"><a id="l00034" name="l00034"></a><span class="lineno">   34</span>        std::size_t cuttingPoint2 = <a class="code hl_function" href="namespaceshark_1_1random.html#aa64d4174eaf7111b03e0504eaa56b666" title="Draws a discrete number in {low,low+1,...,high} by drawing random numbers from rng.">random::discrete</a>(rng,cuttingPoint1+1,n-1);</div>
<div class="line"><a id="l00035" name="l00035"></a><span class="lineno">   35</span> </div>
<div class="line"><a id="l00036" name="l00036"></a><span class="lineno">   36</span>        PointType r1(n, unset), r2(n, unset);</div>
<div class="line"><a id="l00037" name="l00037"></a><span class="lineno">   37</span>        PointType p1(n, unset), p2(n, unset);</div>
<div class="line"><a id="l00038" name="l00038"></a><span class="lineno">   38</span> </div>
<div class="line"><a id="l00039" name="l00039"></a><span class="lineno">   39</span>        <span class="comment">//swap ranges [cuttingPoint1,cuttingPoint2] and store in p1,p2</span></div>
<div class="line"><a id="l00040" name="l00040"></a><span class="lineno">   40</span>        <span class="comment">//also keep track which elements are already taken and which one are free</span></div>
<div class="line"><a id="l00041" name="l00041"></a><span class="lineno">   41</span>        <span class="keywordflow">for</span>( std::size_t i = cuttingPoint1; i &lt;= cuttingPoint2; i++ ) {</div>
<div class="line"><a id="l00042" name="l00042"></a><span class="lineno">   42</span>            p1[i] = t2[i];</div>
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno">   43</span>            p2[i] = t1[i];</div>
<div class="line"><a id="l00044" name="l00044"></a><span class="lineno">   44</span> </div>
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno">   45</span>            r1[ t2[i] ] = t1[i];</div>
<div class="line"><a id="l00046" name="l00046"></a><span class="lineno">   46</span>            r2[ t1[i] ] = t2[i];</div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno">   47</span>        }</div>
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno">   48</span> </div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno">   49</span>        <span class="keywordflow">for</span>( std::size_t i = 0; i &lt; t1.size(); i++) {</div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno">   50</span>            <span class="keywordflow">if</span> ((i &gt;= cuttingPoint1) &amp;&amp; (i &lt;= cuttingPoint2)) <span class="keywordflow">continue</span>;</div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno">   51</span> </div>
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno">   52</span>            std::size_t n1 = t1[i] ;</div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno">   53</span>            std::size_t m1 = r1[n1] ;</div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno">   54</span> </div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno">   55</span>            std::size_t n2 = t2[i] ;</div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno">   56</span>            std::size_t m2 = r2[n2] ;</div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno">   57</span> </div>
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno">   58</span>            <span class="keywordflow">while</span> (m1 != unset) {</div>
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno">   59</span>                n1 = m1 ;</div>
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno">   60</span>                m1 = r1[m1] ;</div>
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno">   61</span>            }</div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno">   62</span>            <span class="keywordflow">while</span> (m2 != unset) {</div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno">   63</span>                n2 = m2 ;</div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno">   64</span>                m2 = r2[m2] ;</div>
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno">   65</span>            }</div>
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno">   66</span>            p1[i] = n1 ;</div>
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno">   67</span>            p2[i] = n2 ;</div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno">   68</span>        }</div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno">   69</span> </div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno">   70</span>        t1 = p1;</div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno">   71</span>        t2 = p2;</div>
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno">   72</span> </div>
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno">   73</span>    }</div>
</div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno">   74</span><span class="comment"></span> </div>
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno">   75</span><span class="comment">    /// \brief Serializes this instance to the supplied archive.</span></div>
<div class="line"><a id="l00076" name="l00076"></a><span class="lineno">   76</span><span class="comment"></span>    <span class="keyword">template</span>&lt;<span class="keyword">typename</span> Archive&gt;</div>
<div class="line"><a id="l00077" name="l00077"></a><span class="lineno"><a class="line" href="structshark_1_1_partially_mapped_crossover.html#ae1de082eed71f2d17e7467a6121ee1cd">   77</a></span>    <span class="keywordtype">void</span> <a class="code hl_function" href="structshark_1_1_partially_mapped_crossover.html#ae1de082eed71f2d17e7467a6121ee1cd" title="Serializes this instance to the supplied archive.">serialize</a>( Archive &amp;, <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> ) {}</div>
<div class="line"><a id="l00078" name="l00078"></a><span class="lineno">   78</span>};</div>
</div>
<div class="line"><a id="l00079" name="l00079"></a><span class="lineno">   79</span>}</div>
<div class="line"><a id="l00080" name="l00080"></a><span class="lineno">   80</span> </div>
<div class="line"><a id="l00081" name="l00081"></a><span class="lineno">   81</span><span class="preprocessor">#endif</span></div>
</div><!-- fragment --></div><!-- contents -->
</div>
</body>
</html>
